package com.uiao.activemq;

import org.apache.activemq.ActiveMQConnection;
import org.apache.activemq.ActiveMQConnectionFactory;

import javax.jms.*;

/**
 * 订阅发送端
 *
 * Created by cuihao on 2018/3/10.
 */
public class TopicProducer {

    public static void main(String[] args) {
        ConnectionFactory connectionFactory;
        Connection connection = null;
        Session session;
        Destination destination;
        MessageProducer messageProducer;
        connectionFactory = new ActiveMQConnectionFactory(ActiveMQConnection.DEFAULT_BROKER_URL);

        try {
            connection = connectionFactory.createConnection();
            connection.start();
            session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
            destination = session.createTopic("mq-topic");

            messageProducer = session.createProducer(destination);
            messageProducer.setDeliveryMode(DeliveryMode.NON_PERSISTENT);

            MqBean mqBean = new MqBean();
            mqBean.setAge(23);
            for (int i = 0; i < 30; i++) {
                Thread.sleep(100);
                mqBean.setName("崔颢 " + i);
                messageProducer.send(session.createObjectMessage(mqBean));
                System.out.println("发送第 " + i + "个对象");
            }
            messageProducer.close();
            System.out.println("消息发送完成!");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}
